﻿using System;
using Algoritmia.Utils;
using Lists = Algoritmia.DataStructures.Lists;
using System.Linq;

namespace Algoritmia.Problems.GasStations
{
    public class GasStationRoutePlanner
    {
        private readonly Func<Lists.IList<int>> _createList;

        public GasStationRoutePlanner(Func<Lists.IList<int>> createList = null)
        {
            _createList = createList ?? (() => AlgoritmiaFactory.Lists<int>.Default);
        }

        public Lists.IList<int> Plan(Lists.IList<double> d, int n)
        {
            var stops = _createList();
            stops.Add(0);
            double km = n;
            int i = 0;
            foreach(var distance in d)
            {
                if (distance >= km)
                {
                    km = n;
                    stops.Add(i);
                }
                km -= distance;
                i++;
            }
            if ( stops.Last() != d.Count -1)
            {
                stops.Add(d.Count-1);
            }
            return stops;
        }

    }
}
